package fy.form.listen.operate;

import java.util.List;

import com.seeyon.ctp.common.content.affair.AffairManager;
import com.seeyon.v3x.services.V3XLocator;
import com.seeyon.v3x.services.document.DocumentFactory;
import com.seeyon.v3x.services.flow.bean.*;
import com.seeyon.v3x.services.form.bean.FormExport;
import com.seeyon.v3x.services.form.bean.RecordExport;
import com.seeyon.v3x.services.form.bean.SubordinateFormExport;
import com.seeyon.v3x.services.form.bean.ValueExport;

import fy.form.listen.bean.FormBean;

/**
 * 存储表单各个输入项
 * 
 * @author Administrator
 * 
 */
public abstract class AForm_Save_Operator implements IFormOperator {

	private static DocumentFactory documentFactory;
	private static AffairManager affairManager;

	protected FormBean formBean;

	protected PersonExport createPerson;

	protected short flowStatus;

	protected String templeteCode;

	protected boolean success;

	public void setFlowStatus(short flowStatus) {
		this.flowStatus = flowStatus;
	}

	public void setTempleteCode(String templeteCode) {
		this.templeteCode = templeteCode;
	}

	public void operate(long summaryID, long memberID) throws Exception {

		if (documentFactory == null) {
			documentFactory = V3XLocator.getInstance().lookup(
					DocumentFactory.class);
		}
		FlowExport fe = documentFactory.exportFlow2("", summaryID);

		if (affairManager == null) {
			affairManager = V3XLocator.getInstance()
					.lookup(AffairManager.class);
		}

		formBean = new FormBean();

		formBean.setSummaryId(summaryID);
		formBean.setOperatorID(memberID);
		formBean.setFlowExport(fe);

		parse();

		success = save();
	}

	public boolean isSuccess() {
		return success;
	}

	/**
	 * 获取需要的表单数据
	 */
	protected void parse() {

		TextExport content = this.formBean.getFlowExport().getFlowContent();
		createPerson = formBean.getFlowExport().getFlowCreatePerson();

		FormExport form = (FormExport) content;

		parseOtherField_FirstStep(this.formBean.getFlowExport());
		parseFormMain(form.getValues());
		parseFormSon(form.getSubordinateForms());
	}

	private void parseFormMain(List<ValueExport> values) {
		if (values != null) {
			for (int i = 0, size = values.size(); i < size; i++) {

				ValueExport ve = values.get(i);
				if (ve != null) {
					parseValueExport(ve);
				}
			}
		}
	}

	private void parseFormSon(List<SubordinateFormExport> subFormList) {
		if (subFormList != null) {
			for (int i = 0, size = subFormList.size(); i < size; i++) {
				SubordinateFormExport subordinateFormExport = subFormList
						.get(i);

				if (subordinateFormExport != null) {
					List<RecordExport> recordExportList = subordinateFormExport
							.getValues();

					if (recordExportList != null) {
						for (int j = 0, recordSize = recordExportList.size(); j < recordSize; j++) {
							RecordExport recordExport = recordExportList.get(j);

							List<ValueExport> subValueArray = recordExport
									.getRecord();

							if (subValueArray != null) {
								for (int m = 0, subValueArraySize = subValueArray
										.size(); m < subValueArraySize; m++) {

									ValueExport ve = subValueArray.get(m);
									if (ve != null) {
										parseValueExport(ve);
									}
								}
							}
						}
					}
				}
			}
		}
	}

	/**
	 * 如果有其他数据需要读取
	 * 
	 * @param flowExport
	 */
	protected abstract void parseOtherField_FirstStep(FlowExport flowExport);

	/**
	 * 获取字段数据
	 * 
	 * @param ve
	 */
	protected abstract void parseValueExport(ValueExport ve);

	protected abstract boolean save();
}
